// TOWN SCRIPT
//    Town 6: Proving Grounds - Sixth Level

// This is the special encounter script for this town.
// The states INIT_STATE, EXIT_STATE, and START_STATE have
// meanings that are described in the documenation. States you write
// yourself should be numbered from 10-100.

// flags:
// 6, 27 = balanced pre-sets
// 6, 28 = presets all gone
// 6, 29 = generate new monsters

begintownscript;

variables;

short i, j, x, y, choice;

body;

beginstate INIT_STATE;
	turn_off_training(1);
	set_crime_tolerance(1);

	// check monsters...
	run_scenario_script(20);
	if (get_flag(6, 29) > 0) {
		// generate some more...
		set_flag(6, 29, 0);
		print_str_color("Generating monsters...", 3);
		j = get_ran(1, 14, 24);
		i = 0;
		while (i < j) {
			run_scenario_script(32);
			if (get_flag(100, 0) > 0) {
				run_scenario_script(26);
				place_monster(get_flag(100, 1), get_flag(100, 2), get_flag(100, 0), 0);
			}
			i = i + 1;
		}
		run_scenario_script(41);
	}
break;

beginstate EXIT_STATE;
// Always called when the town is left.
break;

beginstate START_STATE;
break;

beginstate 10;
	reset_dialog();
	add_dialog_str(0, "There is an elevator here.", 0);
	add_dialog_str(1, "Use?", 0);
	add_dialog_choice(0, "No");
	add_dialog_choice(1, "Yes");
	choice = run_dialog(0);
	if (choice == 2) {
		block_entry(1);
		i = 0;
		while (i == 0) {
			get_text_response("Which level (A-F), C exits");
			check_text_response_match("A");
			if (got_text_match() > 0) {
				move_to_new_town(4, 24, 43);
				end();
			}
			check_text_response_match("B");
			if (got_text_match() > 0) {
				move_to_new_town(5, 23, 43);
				end();
			}
			check_text_response_match("C");
			if (got_text_match() > 0) {
				block_entry(0);
				// move_to_new_town(6, 24, 43);
				end();
			}
			check_text_response_match("D");
			if (got_text_match() > 0) {
				move_to_new_town(7, 24, 43);
				end();
			}
			check_text_response_match("E");
			if (got_text_match() > 0) {
				move_to_new_town(8, 24, 9);
				end();
			}
			check_text_response_match("F");
			if (got_text_match() > 0) {
				move_to_new_town(9, 24, 43);
				end();
			}
		}
	}
break;

beginstate 11;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a ladder leading up to the previous level.", 0);
	add_dialog_choice(0, "Leave");
	add_dialog_choice(1, "Climb up");
	choice = run_dialog(0);
	if (choice == 2) {
		// ladder back up...
		move_to_new_town(5, 20, 27);
	}
break;

beginstate 12;
	block_entry(1);
	reset_dialog();
	add_dialog_str(0, "There is a ladder leading down into the maze.", 0);
	add_dialog_choice(0, "Leave");
	add_dialog_choice(1, "Climb down");
	choice = run_dialog(0);
	if (choice == 2) {
		// ladder down...
		move_to_new_town(7, 4, 4);
	}
break;

beginstate 13;
	block_entry(1);
	x = char_loc_x(0);
	y = char_loc_y(0);
	if (y >= 40) {
		y = 5;
	}
	else {
		y = 42;
	}
	teleport_party(x, y, 1);
break;

beginstate 14;
	run_scenario_script(11);
break;

beginstate 15;
	// Thundar the Barbarian encounter...
	if (get_flag(6, 0) == 0) {
		set_flag(6, 0, 1);
		reset_dialog();
		add_dialog_str(0, "Ahead of you in the corridor, you see a strange sight.", 0);
		add_dialog_str(1, "A golden haired barbarian on a magnificant stead is shouting orders at some followers.", 0);
		add_dialog_str(2, "_Oookla! Ariel! We ride!_ he thunders.", 0);
		add_dialog_str(3, "With that, they ride out of sight and disappear.", 0);
		add_dialog_choice(0, "OK");
		choice = run_dialog(0);
	}
break;
